{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Curving Grades\n",
    "\n",
    "You'll be playing the part of a teacher, curving test grades to account for an unfortunately difficult test.  The curving should improve the student's grade relative to where they ended up with respect to the group's average.  It should also *not* hurt the student by reducing their grade, and it shouldn't give anyone anything over 100%."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "CURVE_CENTER = 80"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def curve(grades):\n",
    "    \"\"\"Adjusts an array of grades so that the average is roughly shifted\n",
    "    to the specified curve center.\n",
    "\n",
    "    This will never cause a student's grade to decrease, and it will\n",
    "    never cause the final grade to go over 100%.\n",
    "\n",
    "    Parameters:\n",
    "        grades (np.ndarray): The individual student grades, between 0\n",
    "                             and 100.\n",
    "\n",
    "    Returns:\n",
    "        (np.ndarray): A new array of grades, adjusted upwards, but in\n",
    "                      the same order.\n",
    "    \"\"\"\n",
    "    average = grades.mean()\n",
    "    change = CURVE_CENTER - average\n",
    "    new_grades = grades + change\n",
    "    return np.clip(new_grades, grades, 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 91.25,  54.25,  83.25, 100.  ,  70.25, 100.  ,  93.25,  31.25]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grades = np.array([[72, 35, 64, 88, 51, 90, 74, 12]])\n",
    "curve(grades)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note how none of the grades are decreased or greater than 100%!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
